package io.chain.ko.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import io.chain.ko.entity.TUser;
import io.chain.ko.service.UserService;
import io.chain.ko.util.AuthUtils;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	
	@Autowired
	private UserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		TUser tuser=userService.findByUsername(username);
		if(tuser==null) {
			throw new UsernameNotFoundException("用户不存在");
		}		
		UserDetails userDetails=new User(tuser.getUsername(),tuser.getPassword(),AuthUtils.buildAuth(tuser.getRoles()));
		return userDetails;
	}
	
}
